package controller;

import pojo.Manage;
import service.ManageService;
import view.SelfBookDetails;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SelfBookDetailsListener implements ActionListener {
    private SelfBookDetails selfBookDetails;
    private String userId;
    private String userName;

    public SelfBookDetailsListener(SelfBookDetails dialog, String userId, String userName){
        super();
        this.selfBookDetails = dialog;
        this.userId=userId;
        this.userName=userName;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回")) {
            selfBookDetails.dispose();
        }
        if (e.getActionCommand().equals("借阅")) {
            int i = JOptionPane.showConfirmDialog(null, "确认借阅该书吗?","确认",JOptionPane.YES_NO_OPTION);
            if(i == 0) {
                ManageService manageService = new ManageService();
                Manage manage = new Manage(userId, userName, selfBookDetails.getBook().getBookId(), selfBookDetails.getBook().getBookName(), null, null);
                manageService.add(manage);
                selfBookDetails.dispose();
                JOptionPane.showMessageDialog(selfBookDetails, "借阅成功");
            }
        }
    }
}
